############################################################
### Table 3. Measurement Model for Latent Variables, U.S ###
############################################################

load("/Users/Yannan/Desktop/data_us.RData")

library(lavaan)

### model 1
model <- '
# measurement model
cp85 =~ y1 + y2 + y3 + y4 + y5
pc71 =~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12
aff85 =~ a1 + a2 + a3 + a4 + a5
health85 =~ h1 + h2 + h3 

# regressions

cp85 ~ pc71 + aff85 + health85 + AGE.child + SEX.child + EDU.child + married.child + INCOME.child + AGE.parent + SEX.parent + married.parent + INCOME.parent
pc71 ~ SEX.child + AGE.parent + SEX.parent + EDU.parent + married.parent + INCOME.parent
aff85 ~ pc71 + AGE.child + SEX.child + EDU.child + married.child + AGE.parent + SEX.parent + EDU.parent + married.parent
health85 ~ AGE.parent + SEX.parent + INCOME.parent

# residual correlations
y4 ~~ x12
'

fit_us <- sem(model, data = data_us, estimator = "ML", missing = "FIML")
summary(fit_us, standardized = T)
fitMeasures(fit_us)


### model 2
model <- '
# measurement model
cp85 =~ y1 + y2 + y3 + y4 + y5
pc71 =~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12
aff85 =~ a1 + a2 + a3 + a4 + a5
health85 =~ h1 + h2 + h3 

# regressions

cp85 ~ pc71 + aff85 + health85 + SEX.child + EDU.child + SEX.parent + INCOME.parent
pc71 ~ SEX.child + AGE.parent + SEX.parent
aff85 ~ pc71 + SEX.child

# residual correlations
y4 ~~ x12
'

fit_us_2 <- sem(model, data = data_us, estimator = "ML", missing = "FIML")
summary(fit_us_2, standardized = T)
fitMeasures(fit_us_2)